
#include "cmd_func.h"
#include "stdlib.h"
#include "oslib_uart.h"
#include "FreeRTOS.h"
#include "task.h"

#ifdef OSLIB_CAN_MODULE_ENABLED
    #include "oslib_can.h"
#endif

#if defined(OSLIB_UART_MODULE_ENABLED) && USE_OSLIB_UART_CLI
/* 在这里添加命令回调函数的定义或外部声明 */
static void Command_Hello(int argc, char *argv[])
{
    uprintf("Hello SimpleLib!\r\n");
}

static void Command_Echo(int argc, char *argv[])
{
    for (int i = 1; i < argc; i++)
        uprintf("Echo: %s\r\n", argv[i]);
}

#ifdef OSLIB_CAN_MODULE_ENABLED
static void Command_CanSend(int argc, char *argv[])
{
    if (argc >= 3) {
        can_msg msg;
        msg.in[0] = 0x40005850;
        msg.in[1] = 0x00000000;
        uint32_t id = strtol(argv[2], NULL, 16);
        if (argv[1][0] == 's') {
            can_send_msg(id, &msg);
            uprintf("CanSend:std[%x]\r\n", id);
        } else if (argv[1][0] == 'e') {
            can_ext_send_msg(id, &msg);
            uprintf("CanSend:ext[%x]\r\n", id);
        } else
            uprintf("Param Error!\r\n");
    } else {
        uprintf("Param Error!\r\n");
    }
}
#endif

#if ((configUSE_TRACE_FACILITY == 1) && (configUSE_STATS_FORMATTING_FUNCTIONS > 0) && (configSUPPORT_DYNAMIC_ALLOCATION == 1))
static char tasklist_buffer[256];
static void Command_Task(int argc, char *argv[])
{
    uprintf("Free Heap: %dB\r\n", xPortGetFreeHeapSize());
    uprintf("Name          \tState\tPrio\tStack\tRank\r\n");
    vTaskList(tasklist_buffer);
    uprintf(tasklist_buffer);
    // uprintf("Name          \tCount\tUtilization\r\n");
    // vTaskGetRunTimeStats(tasklist_buffer);
    // uprintf(tasklist_buffer);
}
#endif




static void Command_Uart2Forward(int argc, char *argv[])
{
    extern uint8_t wifi_conf_flag;
    if (wifi_conf_flag == 0) {
        wifi_conf_flag = 1;
        uprintf("UART2 Forwarding Start\r\n");
        uprintf_to(&huart2, "+++");
    }
    else {
        wifi_conf_flag = 0;
        uprintf("UART2 Forwarding Stop\r\n");
        uprintf_to(&huart2, "AT+CIPMODE=1\r\n");
    }
}

static void Command_Uart2AT(int argc, char *argv[])
{
    uprintf("Send \"AT\\r\\n\"\r\n");
    uprintf_to(&huart2, "AT\r\n");
}

static void Command_Uart2ATUDPTrans(int argc, char *argv[])
{
    if (argc == 4) {
        uprintf("remoteip=%s, remoteport=%s, localport=%s\r\n", argv[1], argv[2], argv[3]);
        uprintf_to(&huart2, "AT+SAVETRANSLINK=1,\"%s\",%s,\"UDP\",%s\r\n", argv[1], argv[2], argv[3]);
    }
    else {
        uprintf("Param Error!\r\n");
    }
}

static void Command_Uart2ATMode(int argc, char *argv[])
{
    if (argc == 1) {
        uprintf("Send \"AT+CWMODE_DEF?\"\r\n");
        uprintf_to(&huart2, "AT+CWMODE_DEF?\r\n");
    }
    else if (argc == 2) {
        uprintf("Send \"AT+CWMODE_DEF=%s\"\r\n", argv[1]);
        uprintf_to(&huart2, "AT+CWMODE_DEF=%s\r\n", argv[1]);
    }
    else {
        uprintf("Param Error!\r\n");
    }
}

static void Command_Uart2ATConnAP(int argc, char *argv[])
{
    if (argc == 1) {
        uprintf("Send \"AT+CWJAP_DEF?\"\r\n");
        uprintf_to(&huart2, "AT+CWJAP_DEF?\r\n");
    }
    else if (argc == 3) {
        uprintf("Send \"AT+CWJAP_DEF=...<2>\"\r\n");
        uprintf_to(&huart2, "AT+CWJAP_DEF=\"%s\",\"%s\"\r\n", argv[1], argv[2]);
    }
    else if (argc == 4) {
        uprintf("Send \"AT+CWJAP_DEF=...<3>\"\r\n");
        uprintf_to(&huart2, "AT+CWJAP_DEF=\"%s\",\"%s\",\"%s\"\r\n", argv[1], argv[2], argv[3]);
    }
    else {
        uprintf("Param Error!\r\n");
    }
}

static void Command_Uart2ATExitTrans(int argc, char *argv[])
{
    uprintf("Send \"+++\"\r\n");
    uprintf_to(&huart2, "+++");
}

static void Command_Calib(int argc, char *argv[])
{
    extern uint8_t calib_flag;
    extern uint8_t calib_type;
    if (argc == 1)
        calib_type = 0;
    else
        calib_type = atoi(argv[1]);
    if (calib_type != 1) 
        calib_type = 0;
    uprintf("Start Calibration and Type is %d\r\n", calib_type);
    calib_flag = 1;
}

static void Command_Redir(int argc, char *argv[])
{
    extern uint8_t redir_flag;
    if (redir_flag)
    {
        redir_flag = 0;
        uprintf("Stop Output\r\n");
    }
    else
    {
        redir_flag = 1;
        uprintf("Start Output\r\n");
    }
}

/* 在这里完成命令到回调函数的映射 */
void Command_Init()
{
    // OSLIB_UartCmd_AddCommand("hello", "\"Hello SimpleLib!\"", Command_Hello);   /* 输出 "Hello SimpleLib!" */   // 测试串口输出和命令
    OSLIB_UartCmd_AddCommand("echo", "Echo message", Command_Echo);             /* 回显参数的内容 */              // 测试命令参数解析
#ifdef OSLIB_CAN_MODULE_ENABLED
    OSLIB_UartCmd_AddCommand("cansend", "CAN send message", Command_CanSend);   /* 发送CAN报文, 内容为两个整数 */  // 测试CAN发送
#endif
#if ((configUSE_TRACE_FACILITY == 1) && (configUSE_STATS_FORMATTING_FUNCTIONS > 0) && (configSUPPORT_DYNAMIC_ALLOCATION == 1))
    OSLIB_UartCmd_AddCommand("tasks", "Show task list", Command_Task);
#endif
    /* wifi配置相关 */
    OSLIB_UartCmd_AddCommand("wconf", "Start/Stop WiFi Configuration", Command_Uart2Forward);
    OSLIB_UartCmd_AddCommand("wat", "[0]Test AT Command. <no param>", Command_Uart2AT);
    OSLIB_UartCmd_AddCommand("wmode", "[1]Set Wifi Mode. <p1>:1-STA,2-AP", Command_Uart2ATMode);
    OSLIB_UartCmd_AddCommand("wconn", "[2]Connect AP. <p1>:ssid <p2>:passwd", Command_Uart2ATConnAP);
    OSLIB_UartCmd_AddCommand("wtran", "[3]Set Trans Link. <p1>:rip <p2>:rport <p3>:lport", Command_Uart2ATUDPTrans);
    OSLIB_UartCmd_AddCommand("wexit", "[*]Exit Trans Link. <no param>", Command_Uart2ATExitTrans);

    /* 摇杆配置相关 */
    OSLIB_UartCmd_AddCommand("jcali", "Start Joystick Calibration", Command_Calib);
    OSLIB_UartCmd_AddCommand("jout", "Start/Stop Joystick Output", Command_Redir);
}
#endif